<template>
  <div class="box">
            <header class="header">
        <van-nav-bar
          title="购物车"
          left-text="返回"
          left-arrow
          @click-left="$router.push('/home')"
        >
        </van-nav-bar>
      </header>
      <div class="content"> 
        <van-empty image="search" description="购物车"  class="kong" v-if="false"/>

        <div>
       <van-checkbox-group v-model="result" ref="checkboxGroup">
        <div v-for="(itme,i) in carts" :key="i">
        <van-card :price="itme.originprice" :title="itme.proname" :thumb="itme.img1" >
    <template #num>
       <div>
          <van-checkbox :name="itme.cartid" class="col" @click="cheboxclick(itme.cartid,i)" @change='cheboxchange'></van-checkbox>
          <div class="mos2" @click="removes(itme.cartid,i)">删除</div>
      <van-stepper :value="itme.num" integer @plus='jia(itme.cartid,i)' @minus='jian(itme.cartid,i)'/>
  </div>
  </template>

</van-card>
</div>

<van-submit-bar :price="3050" button-text="提交订单" @submit="onSubmit">
<input type="checkbox" v-model="val" @click="checkAll" id="alls"><label for="alls">全选</label>
</van-submit-bar>

</van-checkbox-group>
      </div>







        
      </div>
    </div>
</template>
<script>
import Vue from 'vue';
import {cartadd,cartlist,cartremove,updatenum,selectone,selectall  } from '@/utlis/cart.js';
import { Empty } from 'vant';
import { Card } from 'vant';
import { Stepper } from 'vant';
import { Checkbox, CheckboxGroup } from 'vant';
import { Button } from 'vant';
import { SubmitBar } from 'vant';
import { Toast } from 'vant';

Vue.use(Toast);
Vue.use(SubmitBar);
Vue.use(Button);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Stepper);
Vue.use(Card);
Vue.use(Empty);
export default {
  data(){
    return{
      zt:true,
      result: [],
      carts:[],
      val:false,
    }
  },
  watch:{
    result(){
       cartlist({userid:localStorage.getItem("userid")}).then(res=>{
      let cas=res.data.data
      if(this.result.length==cas.length){//判断全选选中
        this.val=true
      }else{
        this.val=false
      }
       })
    

    },
    val(){
        selectall({userid:localStorage.getItem("userid"),type:this.val}).then(res=>{
        console.log(res);
      })
    }
  },
  methods:{
    cheboxchange(name){//全选按钮
    console.log(name);
    },
    cheboxclick(cartid,i){//单选按钮
      let flag= this.result.some((v)=> v==cartid)
      // console.log(cartid,flag);
      selectone({cartid, flag:flag }).then(res=>{
        console.log(res);
      })
    },
     checkAll() {//全选按钮
      if(this.val==true){
      this.$refs.checkboxGroup.toggleAll(false);
      }
      else {
        this.$refs.checkboxGroup.toggleAll(true);
      }


      console.log(this.result);
    },
    onSubmit(){
    //  this.$router.push("/dingdan")
    Toast('给我半天我能写出来');

    },
    removes(cartid,i){
      cartremove({cartid}).then(res=>{
        console.log(res);
      })
      console.log(i);
      this.carts.splice(i,1)
      console.log(this.carts);
    },
    jia(cartid,ins){
      updatenum({cartid,num:++this.carts[ins].num}).then(res=>{
        console.log(res);

      })

    },
    jian(cartid,ins){
            updatenum({cartid,num:--this.carts[ins].num}).then(res=>{
        console.log(res);

      })
    },

  },
  mounted(){
    cartlist({userid:localStorage.getItem("userid")}).then(res=>{
      let cas=res.data.data
      if(cas){
      cas=cas.map((v,i)=>{
        v.originprice=v.originprice+""
        return v
      })
      this.carts=cas
      console.log(this.carts);

      cas.forEach((v) => {if(v.flag==true){this.result.push(v.cartid)}});//判断单选

      if(cas.every((v)=>v.flag==true)){//判断全选选中
        this.val=true
      }else{
        this.val=false
      }
      }

      
    })
    

  }
  
}
</script>
<style lang='stylus' scoped>
.kong{
  position: absolute
  left 50%
  top 50%
  transform: translate(-50%,-50%);
}

.purchasenum{
  position relative
  left -20vw
  top 0
}
.minus{
  width 20px
  height 20px
  border: 1px solid black
  font-size 14px
  text-align: center
  line-height: 20px
  position absolute
  left 0
  top 0

}
.plus{
  width 20px
  height 20px
  border: 1px solid black
  text-align: center
  font-size 14px
  line-height: 20px
  position absolute
  left 56px
  top 0

}
.purchinput{
  width 30px
  height 18px
  border: 1px solid black
  position: absolute;
  left: 21px;
  top: 0;
  text-align: center
  

}
.col{
  position absolute
  left 60vw
  top 6vw
}
.mos2{
  background-color: #ccc
  position absolute
  top 6vw
  right 10vw
  color: black
}

</style>
